package org.csu.mypetstorebackstage.controller;

import org.csu.mypetstorebackstage.domain.Category;
import org.csu.mypetstorebackstage.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    final String current = "category";
    @GetMapping("/getCategoryView")
    public String getCategoryView(Model model)
    {
        List<Category> categoryList = categoryService.getCategoryAll();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("current", current);
        System.out.println(categoryList);
        return "category/category";
    }

    @PostMapping("/getSpecialCategory")
    public String getSpecialCategory(Model model,String categoryNameSearch)
    {
        model.addAttribute("current", current);
        if(categoryNameSearch.equals("input the name of category to search"))
        {
            List<Category> categoryList = categoryService.getCategoryAll();
            model.addAttribute("categoryList",categoryList);
            System.out.println(categoryList);
            return "category/category";
        }
        else
        {
            Category category = categoryService.getCategoryByName(categoryNameSearch);
            List<Category> categoryList = new ArrayList<>();
            categoryList.add(category);
            model.addAttribute("categoryList",categoryList);
            return "category/category";
        }
    }

    @GetMapping("/returnMain")
    public String returnMain()
    {

        return "Visual/VisualMain";
    }


}
